package org.zhq.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhq.ServerResult;
import org.zhq.enums.YesOrNo;
import org.zhq.pojo.*;
import org.zhq.pojo.vo.*;
import org.zhq.service.CarouselService;
import org.zhq.service.CategoryService;
import org.zhq.service.ItemsService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(value = "商品", tags = {"商品展示相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController {

    private final ItemsService itemsService;

    @Autowired
    public ItemsController(ItemsService itemsService) {
        this.itemsService = itemsService;
    }

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public ServerResult info(@PathVariable String itemId) {
        if (StrUtil.isBlank(itemId)) {
            return ServerResult.errorMsg("商品不存在");
        }
        Items items = itemsService.queryItemsById(itemId);
        List<ItemsImg> itemsImgList = itemsService.queryItemsImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemsService.queryItemsSpecList(itemId);
        ItemsParam itemsParam = itemsService.queryItemsParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return ServerResult.ok(itemInfoVO);
    }


    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public ServerResult commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId) {
        if (StrUtil.isBlank(itemId)) {
            return ServerResult.errorMsg("商品不存在");
        }
        CommentLevelCountsVO countsVO = itemsService.queryCommentCounts(itemId);
        return ServerResult.ok(countsVO);
    }

    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public ServerResult comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级")
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "页码")
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize", value = "页距")
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        if (StrUtil.isBlank(itemId)) {
            return ServerResult.errorMsg("商品不存在");
        }
        IPage<ItemCommentVO> pageResult = itemsService.queryPagedComments(itemId, level, page, pageSize);
        return ServerResult.ok(pageResult);
    }


    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public ServerResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序方式")
            @RequestParam String sort,
            @ApiParam(name = "page", value = "页码")
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize", value = "页距")
            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        if (StrUtil.isBlank(keywords)) {
            return ServerResult.errorMsg("商品不存在");
        }
        IPage<SearchItemsVO> pageResult = itemsService.searchItems(keywords, sort, page, pageSize);
        return ServerResult.ok(pageResult);
    }

    @ApiOperation(value = "通过分类id搜索商品列表", notes = "通过分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public ServerResult catItems(
            @ApiParam(name = "catId", value = "分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序方式")
            @RequestParam String sort,
            @ApiParam(name = "page", value = "页码")
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize", value = "页距")
            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        if (catId == null) {
            return ServerResult.errorMsg("商品不存在");
        }
        IPage<SearchItemsVO> pageResult = itemsService.searchItemsByThirdCat(catId, sort, page, pageSize);
        return ServerResult.ok(pageResult);
    }

    @ApiOperation(value = "刷新购物车商品信息", notes = "刷新购物车商品信息", httpMethod = "GET")
    @GetMapping("/refresh")
    public ServerResult refresh(
            @ApiParam(name = "itemSpecIds", value = "商品规格id", required = true)
            @RequestParam String itemSpecIds) {
        if (StrUtil.isBlank(itemSpecIds)) {
            return ServerResult.errorMsg(null);
        }
        String[] specIds = itemSpecIds.split(",");
        List<ItemsSpec> itemsSpecList = itemsService.queryItemSpecListByIds(Arrays.asList(specIds));
        List<ShopCartVO> shopCartList = new ArrayList<>();
        for (ItemsSpec itemsSpec : itemsSpecList) {
            ShopCartVO shopCartVO = new ShopCartVO();
            String itemId = itemsSpec.getItemId();
            shopCartVO.setItemId(itemId);
            Items items = itemsService.queryItemsById(itemId);
            shopCartVO.setItemName(items.getItemName());
            String imgUrl = itemsService.queryItemMainImgById(itemId);
            shopCartVO.setItemImgUrl(imgUrl);
            shopCartVO.setPriceDiscount(itemsSpec.getPriceDiscount());
            shopCartVO.setPriceNormal(itemsSpec.getPriceNormal());
            shopCartVO.setSpecId(itemsSpec.getId());
            shopCartVO.setSpecName(itemsSpec.getName());
            shopCartList.add(shopCartVO);
        }
        return ServerResult.ok(shopCartList);
    }
}
